/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package gui.Ventas;

import Atxy2k.CustomTextField.RestrictedTextField;
import java.text.DecimalFormat;
import java.text.DecimalFormatSymbols;
import javax.swing.JDialog;
import Controlador.Bean.Ventas.NotaCreditoBusqueda;
import Controlador.Service.Ventas.GestionVentas;
import java.util.ArrayList;
import java.util.Date;
import javax.swing.table.AbstractTableModel;
import javax.swing.table.TableRowSorter;

/**
 *
 * @author Luis Enrique
 */
public class NotaCreditoBuscarPanel extends javax.swing.JPanel {

    DevTableModel myTableModel;
    private JDialog parent;
    private FacturacionRegistrarPanel refPanel;
    private String CodCliente;
    Date fecha = new Date ();
    ArrayList<NotaCreditoBusqueda> devsConsulta = new ArrayList<>();
    //Formato decimal
    DecimalFormatSymbols simbolos = new DecimalFormatSymbols();
    DecimalFormat df; 
    
    public void setParent(JDialog parent){
        this.parent = parent;
    }
    
    public void setNC(FacturacionRegistrarPanel refPanel){
        this.refPanel = refPanel;
    }
    
    public void setCliente(String codigo){
        this.CodCliente = codigo;
    }     
       
    class DevTableModel extends AbstractTableModel{
        String [] titles = {"Número de NC", "Número de Devolución","Razón Social/Nombre","Monto Total"};

        @Override
        public int getRowCount() {
            return devsConsulta.size();
        }

        @Override
        public int getColumnCount() {
            return titles.length;
        }

        @Override
        public Object getValueAt(int rowIndex, int columnIndex) {
            String res=null;
            NotaCreditoBusqueda db = devsConsulta.get(rowIndex);
            switch(columnIndex){
                case 0: res = db.getIdNotaCredito(); break;
                case 1: res = db.getIdDevolucion();break;
                case 2: res = (db.getRazonSocial()!=null)?db.getRazonSocial():(db.getApePaterno()+" "+db.getApeMaterno()+", "+db.getNombres()); break;
                case 3: res = df.format(db.getMonto()); break;
            }
            return res;
        }
        
        @Override
        public String getColumnName(int col){
            return titles[col];
        }
    }
    
    public NotaCreditoBuscarPanel() {
        initComponents();       
        
        this.simbolos.setDecimalSeparator('.');
        this.df = new DecimalFormat("#.00",simbolos);
        
        RestrictedTextField restricted = new RestrictedTextField(txtNumDev);
        restricted.setLimit(9);
        restricted.setOnlyNums(true);
        
        restricted = new RestrictedTextField(txtNumeroNC);
        restricted.setLimit(9);
        restricted.setOnlyNums(true);
        
        myTableModel = new DevTableModel();
        tbDevs.setModel(myTableModel);
        TableRowSorter<DevTableModel> sorter = new TableRowSorter<DevTableModel> ((DevTableModel) tbDevs.getModel());
        tbDevs.setRowSorter(sorter);
    }
    
    public void refreshItemsTable2(ArrayList<NotaCreditoBusqueda> ncBusq){
        this.devsConsulta = new ArrayList(ncBusq);
        myTableModel.fireTableChanged(null);        
    }
    
    public void limpiarCriterios(){
        txtNumeroNC.setText("");
        txtNumDev.setText("");
        dpFechaReg.setDate(null);
        dpFechaVenc.setDate(null);
    }
    
    public void actualizaTabla(){
        NotaCreditoBusqueda nc = new NotaCreditoBusqueda(null,null,null,null,0,"Registrado",CodCliente,null,null,null,null,null,null,null,null,null);
        this.devsConsulta = GestionVentas.getInstance().queryNC(nc); 
        myTableModel.fireTableChanged(null); 
    }
       
    /**
     * This method is called from within the constructor to initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is always
     * regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        jPanel4 = new javax.swing.JPanel();
        jPanel1 = new javax.swing.JPanel();
        jLabel8 = new javax.swing.JLabel();
        txtNumeroNC = new javax.swing.JTextField();
        txtNumDev = new javax.swing.JTextField();
        jLabel9 = new javax.swing.JLabel();
        jLabel10 = new javax.swing.JLabel();
        dpFechaVenc = new org.jdesktop.swingx.JXDatePicker();
        jLabel11 = new javax.swing.JLabel();
        dpFechaReg = new org.jdesktop.swingx.JXDatePicker();
        btnRegresar = new javax.swing.JButton();
        btnBuscar = new javax.swing.JButton();
        jPanel3 = new javax.swing.JPanel();
        jScrollPane2 = new javax.swing.JScrollPane();
        tbDevs = new javax.swing.JTable();

        setMaximumSize(new java.awt.Dimension(1231231, 13123123));
        setMinimumSize(new java.awt.Dimension(0, 0));

        jPanel4.setBorder(javax.swing.BorderFactory.createTitledBorder("Criterios de Búsqueda"));

        jPanel1.setBorder(javax.swing.BorderFactory.createTitledBorder("Información de la Nota de Crédito"));
        jPanel1.setMaximumSize(new java.awt.Dimension(340, 193));
        jPanel1.setMinimumSize(new java.awt.Dimension(340, 193));

        jLabel8.setText("Número");

        jLabel9.setText("Núm. Devolución");

        jLabel10.setText("Fecha de Registro");

        jLabel11.setText("Fecha de Vencimiento");

        javax.swing.GroupLayout jPanel1Layout = new javax.swing.GroupLayout(jPanel1);
        jPanel1.setLayout(jPanel1Layout);
        jPanel1Layout.setHorizontalGroup(
            jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, jPanel1Layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(jLabel8)
                    .addComponent(jLabel9)
                    .addComponent(jLabel10)
                    .addComponent(jLabel11))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, 148, Short.MAX_VALUE)
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING, false)
                    .addComponent(dpFechaVenc, javax.swing.GroupLayout.Alignment.TRAILING, javax.swing.GroupLayout.DEFAULT_SIZE, 200, Short.MAX_VALUE)
                    .addComponent(txtNumDev, javax.swing.GroupLayout.Alignment.TRAILING)
                    .addComponent(txtNumeroNC, javax.swing.GroupLayout.Alignment.TRAILING)
                    .addComponent(dpFechaReg, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
                .addContainerGap())
        );
        jPanel1Layout.setVerticalGroup(
            jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel1Layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLabel8)
                    .addComponent(txtNumeroNC, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(txtNumDev, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(jLabel9))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLabel10)
                    .addComponent(dpFechaReg, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(dpFechaVenc, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(jLabel11))
                .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
        );

        btnRegresar.setText("Limpiar");
        btnRegresar.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btnRegresarActionPerformed(evt);
            }
        });

        btnBuscar.setText("Buscar");
        btnBuscar.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btnBuscarActionPerformed(evt);
            }
        });

        javax.swing.GroupLayout jPanel4Layout = new javax.swing.GroupLayout(jPanel4);
        jPanel4.setLayout(jPanel4Layout);
        jPanel4Layout.setHorizontalGroup(
            jPanel4Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel4Layout.createSequentialGroup()
                .addGroup(jPanel4Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING, false)
                    .addGroup(jPanel4Layout.createSequentialGroup()
                        .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                        .addComponent(btnBuscar)
                        .addGap(18, 18, 18)
                        .addComponent(btnRegresar))
                    .addGroup(javax.swing.GroupLayout.Alignment.LEADING, jPanel4Layout.createSequentialGroup()
                        .addContainerGap()
                        .addComponent(jPanel1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)))
                .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
        );
        jPanel4Layout.setVerticalGroup(
            jPanel4Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel4Layout.createSequentialGroup()
                .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                .addComponent(jPanel1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                .addGroup(jPanel4Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(btnBuscar)
                    .addComponent(btnRegresar))
                .addGap(10, 10, 10))
        );

        jPanel3.setBorder(javax.swing.BorderFactory.createTitledBorder("Lista de Notas de Crédito"));
        jPanel3.setToolTipText("");
        jPanel3.setMaximumSize(new java.awt.Dimension(764, 305));
        jPanel3.setMinimumSize(new java.awt.Dimension(764, 305));
        jPanel3.setPreferredSize(new java.awt.Dimension(764, 305));
        jPanel3.setLayout(new java.awt.BorderLayout());

        tbDevs.setModel(new javax.swing.table.DefaultTableModel(
            new Object [][] {
                {null, null, null, null},
                {null, null, null, null},
                {null, null, null, null},
                {null, null, null, null}
            },
            new String [] {
                "Title 1", "Title 2", "Title 3", "Title 4"
            }
        ));
        tbDevs.addMouseListener(new java.awt.event.MouseAdapter() {
            public void mouseClicked(java.awt.event.MouseEvent evt) {
                tbDevsMouseClicked(evt);
            }
        });
        jScrollPane2.setViewportView(tbDevs);

        jPanel3.add(jScrollPane2, java.awt.BorderLayout.CENTER);

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(this);
        this.setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(layout.createSequentialGroup()
                        .addComponent(jPanel4, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addGap(0, 0, Short.MAX_VALUE))
                    .addComponent(jPanel3, javax.swing.GroupLayout.Alignment.TRAILING, javax.swing.GroupLayout.PREFERRED_SIZE, 0, Short.MAX_VALUE))
                .addContainerGap())
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
                .addContainerGap()
                .addComponent(jPanel4, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                .addComponent(jPanel3, javax.swing.GroupLayout.PREFERRED_SIZE, 190, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
        );
    }// </editor-fold>//GEN-END:initComponents

    private void btnRegresarActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btnRegresarActionPerformed
        parent.dispose();
    }//GEN-LAST:event_btnRegresarActionPerformed

    private void tbDevsMouseClicked(java.awt.event.MouseEvent evt) {//GEN-FIRST:event_tbDevsMouseClicked
        if(evt.getClickCount()==2){
            int rowSel = tbDevs.getSelectedRow();
            refPanel.ncb = devsConsulta.get(rowSel);     
            refPanel.llenaNC();
            refPanel.actualizaDolares();
            parent.dispose();           
        }
    }//GEN-LAST:event_tbDevsMouseClicked

    private void btnBuscarActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btnBuscarActionPerformed
        String numNC = (txtNumeroNC.getText().equals(""))?null:txtNumeroNC.getText();
        String numDev = (txtNumDev.getText().equals(""))?null:txtNumDev.getText();
        Date fechaReg = (dpFechaReg.getDate()==null)?null:dpFechaReg.getDate();
        Date fechaVenc = (dpFechaVenc.getDate()==null)?null:dpFechaVenc.getDate();
        
        NotaCreditoBusqueda ncBusqueda2 = new NotaCreditoBusqueda(numNC,numDev,fechaReg,fechaVenc,0,"Registrado",CodCliente,null,null,null,null,null,null,null,null,null);
        ArrayList<NotaCreditoBusqueda> notasBusq = GestionVentas.getInstance().queryNC(ncBusqueda2);
        refreshItemsTable2(notasBusq);
    }//GEN-LAST:event_btnBuscarActionPerformed

    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JButton btnBuscar;
    private javax.swing.JButton btnRegresar;
    private org.jdesktop.swingx.JXDatePicker dpFechaReg;
    private org.jdesktop.swingx.JXDatePicker dpFechaVenc;
    private javax.swing.JLabel jLabel10;
    private javax.swing.JLabel jLabel11;
    private javax.swing.JLabel jLabel8;
    private javax.swing.JLabel jLabel9;
    private javax.swing.JPanel jPanel1;
    private javax.swing.JPanel jPanel3;
    private javax.swing.JPanel jPanel4;
    private javax.swing.JScrollPane jScrollPane2;
    private javax.swing.JTable tbDevs;
    private javax.swing.JTextField txtNumDev;
    private javax.swing.JTextField txtNumeroNC;
    // End of variables declaration//GEN-END:variables
}
